//
//  LYLJumpInterfaceView.m
//  Diary
//
//  Created by lanou3g on 15/11/9.
//  Copyright © 2015年 王艺拓. All rights reserved.
//

#import "LYLJumpInterfaceView.h"

#define kHomeButtonSide 40 //最下面那个跳转到里面的主按钮

@implementation LYLJumpInterfaceView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.leftImg = [[UIImageView alloc] initWithFrame:CGRectMake(kHomeButtonSide, 0, kHomeButtonSide, kHomeButtonSide)];
        self.leftImg.image = [UIImage imageNamed:@"sandtimer_circled"];
        self.leftImg.alpha = 0;
        [self addSubview:self.leftImg];
        
        self.rightImg = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth - kHomeButtonSide*2), 0, kHomeButtonSide, kHomeButtonSide)];
        self.rightImg.image = [UIImage imageNamed:@"book_circled"];
        self.rightImg.alpha = 0;
        [self addSubview:self.rightImg];
    
        self.homeKeyImg = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth - kHomeButtonSide) / 2, 0, kHomeButtonSide, kHomeButtonSide)];
        self.homeKeyImg.image = [UIImage imageNamed:@"home_circle"];
        self.homeKeyImg.userInteractionEnabled = YES;
        [self addSubview:self.homeKeyImg];
    }
    return self;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.leftImg.alpha = 1.0f;
    self.rightImg.alpha = 1.0f;
    self.homeKeyImg.alpha = 0.5;
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.homeKeyImg.alpha = 0.2f;
    CGPoint endPoint =[[touches anyObject] locationInView:self];
    self.homeKeyImg.frame = CGRectMake(endPoint.x , 0, kHomeButtonSide, kHomeButtonSide);
    if (endPoint.x <= kHomeButtonSide) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"PRESENTTOCHILDCONTROLLER" object:@"turnLeft"];
        [UIView animateWithDuration:0.1 animations:^{
            self.leftImg.alpha = 0.0f;
            self.rightImg.alpha = 0.0f;
            self.homeKeyImg.alpha = 1.0f;
            self.homeKeyImg.frame = CGRectMake((kScreenWidth - kHomeButtonSide) / 2, 0, kHomeButtonSide, kHomeButtonSide);
        }];
    } else if (endPoint.x >= (kScreenWidth - kHomeButtonSide*2)) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"PRESENTTOCHILDCONTROLLER" object:@"turnRight"];
        [UIView animateWithDuration:0.1 animations:^{
            self.leftImg.alpha = 0.0f;
            self.rightImg.alpha = 0.0f;
            self.homeKeyImg.alpha = 1.0f;
            self.homeKeyImg.frame = CGRectMake((kScreenWidth - kHomeButtonSide) / 2, 0, kHomeButtonSide, kHomeButtonSide);
        }];
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.leftImg.alpha = 0.0f;
    self.rightImg.alpha = 0.0f;
    self.homeKeyImg.alpha = 1.0f;
    
    [UIView animateWithDuration:0.1 animations:^{
        self.homeKeyImg.frame = CGRectMake((kScreenWidth - kHomeButtonSide) / 2, 0, kHomeButtonSide, kHomeButtonSide);
    }];
}

@end
